package com.ruoyi.wms.controller;

import java.util.List;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.wms.domain.WmsInventory;
import com.ruoyi.wms.service.IWmsInventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.wms.domain.WmsStockIn;
import com.ruoyi.wms.service.IWmsStockInService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 入库管理Controller
 * 
 * @author ruoyi
 * @date 2021-11-07
 */
@RestController
@RequestMapping("/wms/stockIn")
public class WmsStockInController extends BaseController
{
    @Autowired
    private IWmsStockInService wmsStockInService;


    @Autowired
    private IWmsInventoryService wmsInventoryService;

    /**
     * 查询入库管理列表
     */
    @PreAuthorize(hasPermi = "wms:stockIn:list")
    @GetMapping("/list")
    public TableDataInfo list(WmsStockIn wmsStockIn)
    {
        startPage();
        List<WmsStockIn> list = wmsStockInService.selectWmsStockInList(wmsStockIn);
        return getDataTable(list);
    }

    /**
     * 导出入库管理列表
     */
    @PreAuthorize(hasPermi = "wms:stockIn:export")
    @Log(title = "入库管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsStockIn wmsStockIn) throws IOException
    {
        List<WmsStockIn> list = wmsStockInService.selectWmsStockInList(wmsStockIn);
        ExcelUtil<WmsStockIn> util = new ExcelUtil<WmsStockIn>(WmsStockIn.class);
        util.exportExcel(response, list, "入库管理数据");
    }

    /**
     * 获取入库管理详细信息
     */
    @PreAuthorize(hasPermi = "wms:stockIn:query")
    @GetMapping(value = "/{stockInUuid}")
    public AjaxResult getInfo(@PathVariable("stockInUuid") String stockInUuid)
    {
        return AjaxResult.success(wmsStockInService.selectWmsStockInByStockInUuid(stockInUuid));
    }

    /**
     * 新增入库管理
     */
    @Transactional
    @PreAuthorize(hasPermi = "wms:stockIn:add")
    @Log(title = "入库管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmsStockIn wmsStockIn)
    {
        int a = wmsStockInService.insertWmsStockIn(wmsStockIn);
        return toAjax(a);
    }

    /**
     * 更新库存
     */
    @Transactional
    @PreAuthorize(hasPermi = "transfer:transfer:remove")
    @Log(title = "入库单(更新库存)", businessType = BusinessType.UPDATE)
    @GetMapping(value = "/upd-inventory/{stockInUuid}")
    public AjaxResult updInventory(@PathVariable("stockInUuid") String stockInUuid)
    {
        // 取数据
        WmsStockIn t = wmsStockInService.selectWmsStockInByStockInUuid(stockInUuid);

        // 处理记录数（行数）
        AtomicInteger result = new AtomicInteger();

        // 遍历明细
        t.getWmsStockInDetailsList().forEach(d -> {

            WmsInventory i = new WmsInventory();

            // 加库存
            i.setUpdateAt(DateUtils.getNowDate());
            i.setFkItemUuid(d.getFkItemUuid());
            i.setFkStockUuid(t.getFkStockUuid());
            i.setFkStockPosUuid(d.getFkStockPosUuid());
            i.setInventoryQty(d.getDifferentQty());
            if( "pass".equals(t.getOaDictConfirmStatus())){
                wmsInventoryService.insertWmsInventory(i);
            }

            // 记录处理行数
            result.getAndIncrement();

        });

        // 返回处理的记录数
        return toAjax(result.get());
    }


    /**
     * 修改入库管理
     */
    @PreAuthorize(hasPermi = "wms:stockIn:edit")
    @Log(title = "入库管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsStockIn wmsStockIn)
    {
        return toAjax(wmsStockInService.updateWmsStockIn(wmsStockIn));
    }

    /**
     * 删除入库
     */
    @PreAuthorize(hasPermi = "wms:stockIn:remove")
    @Log(title = "入库管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{stockInUuids}")
    public AjaxResult remove(@PathVariable String[] stockInUuids)
    {
        return toAjax(wmsStockInService.deleteWmsStockInByStockInUuids(stockInUuids));
    }
}
